<template>
	<view>
		<view class="dow" @click="createTxt">
			创建hello.txt文件
		</view>
		<view class="dow" @click="writeTxt">
			向txt文件写入数据:欢快的写代码吧！
		</view>
		<view class="dow" @click="getTxtInfo">
			读取txt文件数据:{{info}}
		</view>
		<view class="dow">
			<text @click="downLoad('new')">下载到new</text>
			<text @click="createCatalogue('new')">创建new</text>
			<text @click="createCatalogue('newtoFile')">创建newtoFile</text>
			<text style="margin-left: 40rpx;" @click="downLoad('newtoFile')">下载到newtoFile</text>
		</view>
		<view class="dow">
			<text>new文件列表</text>
			<text style="margin-left: 40rpx;color: red;" @click="removeFile('new','',true)">清空</text>
			<text style="margin-left: 40rpx;color:#fff;" @click="getZip('new','zip')">创建新文件并压缩</text>
			<text @click="delCatalogue('new')">删除目录</text>
		</view>

		<scroll-view scroll-y style="height: 400rpx;background-color: aqua;">
			<view class="item" v-for="(item,index) in fileList" :key="index">
				<view>文件名:{{item.name}}</view>
				<view>路径:{{item.fullPath}}</view>
				<view class="end">
					<view class="left" @click="removeFile('new',item.name,false)">
						删除
					</view>
					<view @click="getFileInfo(item.fullPath)">
						查看文件信息
					</view>
					<view class="right" @click="moveFile('new',item)">
						移动到newtoFile文件夹下
					</view>
				</view>

			</view>
		</scroll-view>

		<view class="dow">
			<text>newtoFile文件列表</text>
			<text style="margin-left: 40rpx;color: red;" @click="removeFile('newtoFile','',true)">清空</text>
			<text style="margin-left: 40rpx;color:#fff;" @click="getZip('newtoFile','zip')">创建新文件并压缩</text>
			<text @click="delCatalogue('newtoFile')">删除目录</text>
		</view>
		<scroll-view scroll-y style="height: 400rpx;background-color: aqua;">
			<view class="item" v-for="(item,index) in newFileList" :key="index">
				<view>文件名:{{item.name}}</view>
				<view>路径:{{item.fullPath}}</view>
				<view class="end">
					<view class="left" @click="removeFile('newtoFile',item.name,false)">
						删除
					</view>
					<view @click="getFileInfo(item.fullPath)">
						查看文件信息
					</view>
					<view class="right" @click="moveFile('newtoFile',item)">
						移动回new文件夹下
					</view>
				</view>

			</view>
		</scroll-view>
		<!-- <view class="dow" @click="seeIsHave">查看指定文件是否存在</view> -->
	</view>
</template>

<script>
	import {
		login,
		upPay,
		getExerciseInfo
	} from '../../api/index.js'; //引入需要的api
	import {
		getFileData,
		getFileList,
		delFileList,
		multifileCopyFile,
		copyFile,
		createTask,
		zipCompress,
		delDirectory,
		creatDirectory,
		createFile,
		writeFile
	} from "@/common/file.js";
	import fileApi from "@/uni_modules/hbh-fileIo/js_sdk/index.js"
	// 以下路径需根据项目实际情况填写
	import {
		pathToBase64,
		base64ToPath
	} from '../../js_sdk/mmmm-image-tools/index.js'


	// ab()


	export default {
		name: "one",
		//1.通过props接收父组件中传过来的值
		props: ["userInfo"],
		data() {
			return {
				deviceId: "",
				text: "",
				audioList: [], //获取所有音频地址
				fileList: [], //原文件
				newFileList: [], //移动目标目录文件
				urlAddress: 'file://storage/emulated/0/Android/data/io.dcloud.HBuilder/documents/',
				obj: null,
				info: "",
			};
		},
		created() {
			this.seeFileList()
			this.seeTargetFileList()
		},
		methods: {
			getTxtInfo() {
		this.obj.file(function(file) {
			resolve(file);
		});
			},
			async writeTxt() {
				console.log(this.obj)
				let res = await writeFile(this.obj, '欢快的写代码吧')
				if (res.code == 200) {
					console.log(res.msg, res.info)
				}
			},
			async createTxt() {
				let res = await createFile(this.urlAddress + 'hello.txt')
				if (res.code == 200) {
					this.obj = res.obj
					this.info=res.info
					console.log(res.msg, res.obj, this.obj,res.info)
				} else {
					console.log(res.msg)
				}
			},
			//创建目录
			async createCatalogue(types) {
				let res = await fileApi.creatDirectory(types)
				if (res.code == 200) {
					console.log(res.msg)
				} else {
					console.log(res.msg)
				}
			},
			//删除目录
			async delCatalogue(types) {
				let res = await fileApi.delDirectory(types)
				if (res.code == 200) {
					console.log(res.msg)
				} else {
					console.log(res.msg)
				}
			},
			//压缩文件的到zip
			async getZip(types, newFileName) { //newFileName:压缩文件存放的目录名称
				let res = await fileApi.zipCompress(types, newFileName + '/' +
					types + '.zip')
				if (res.code == 200) {
					console.log('压缩文件地址：' + res.fullPath)
				} else {
					console.log(res.msg)
				}
			},
			//删除文件
			async removeFile(path, fileName, isAll) { //fileName:不为''则是单文件删除，反之全删
				let res = await fileApi.delFileList(path, isAll, fileName)
				if (res.code == 200) {
					if (path == 'new') {
						this.seeFileList()
					} else {
						this.seeTargetFileList()
					}
				} else {
					console.log(res.msg)
				}
			},
			//查看文件信息
			async getFileInfo(path) {
				let res = await fileApi.getFileData('file://' + path)
				if (res.code == 200) {
					console.log(res.result)
				} else {
					console.log(res.msg)
				}
			},
			//查看文件目录列表
			async seeFileList() {
				let res = await fileApi.getFileList('new')
				if (res.code == 200) {
					this.fileList = res.data
				} else {
					console.log(res.msg)
				}
			},
			//查看移动文件目录列表
			async seeTargetFileList() {
				let res = await fileApi.getFileList('newtoFile')
				if (res.code == 200) {
					this.newFileList = res.data
				} else {
					console.log(res.msg)
				}
			},
			//移动文件到new文件夹下
			async moveFile(types, item) {
				let res
				if (types == 'new') {
					res = await fileApi.multifileCopyFile(types, 'newtoFile') //多文件拷贝
				} else {
					res = await fileApi.multifileCopyFile(types, 'new') //多文件拷贝
				}
				if (res.code == 200) {
					console.log('拷贝成功的文件新地址:' + res.succeedData, '拷贝失败的原文件地址:' + res.failedData)
					this.seeFileList()
					this.seeTargetFileList()
				} else {
					console.log(res.msg)
				}

			},
			//下载
			downLoad(fileName) {
				getExerciseInfo(2209237618).then(async (res) => {
					if (res.code == 200) {
						res.data.questiontypeList.forEach((item, index) => {
							item.questionList.forEach((mak, kpl) => {
								JSON.parse(mak.view).play.forEach((child, opo) => {
									if (child.type == 'audio') {
										this.audioList.push(child.src)
									}
								})
							})
						})
						let obj = await fileApi.createTask(this.audioList[0], fileName)
						if (obj.code == 200) {
							if (fileName == 'new') {
								this.seeFileList()
							} else {
								this.seeTargetFileList()
							}
						} else {
							console.log(obj.msg)
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.end {
		display: flex;
		align-items: center;
		justify-content: space-between;

		.left {
			color: red;
			background-color: #ccc;
			padding: 0rpx 10rpx;
			border-radius: 5rpx;
		}

		.right {
			color: #5ECD8D;
			background-color: #fff;
			padding: 0rpx 10rpx;
			border-radius: 5rpx;
		}
	}

	.dow {
		height: 100rpx;
		background-color: green;
		line-height: 100rpx;
		text-align: center;
		border-bottom: 1rpx solid #ccc;
	}

	.item {
		box-sizing: border-box;
		padding: 10rpx;
		border-bottom: 1px solid #ccc;
	}

	button {
		margin-bottom: 20rpx;
	}
</style>
